package com.apexsoft.monitor.front.console.app.om;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.utils.SpringContextUtil;
import com.apexsoft.monitor.front.console.app.service.AppService;
import com.apexsoft.monitor.front.console.host.om.Host;
import com.apexsoft.monitor.front.console.host.service.HostService;
import com.apexsoft.monitor.front.console.task.om.TaskNew;

import java.util.ArrayList;
import java.util.List;

public class AppNodeNew {
    private String nodeCode;
    private String nodeName;
    private String nodeType;
    private String hostCode;
    private String appCode;
    private String protocol;
    private String basePath;
    private String servicePort;
    private String url;
    private String dbName;
    private String dbUser;
    private String dbPwd;
    private String redisPwd;
    private String remark;
    private int status;

    private List<TaskNew> tasks = new ArrayList<>();

    public AppNodeNew() {
    }

    public AppNodeNew(JSONObject nodeJson) {
        this.nodeCode = nodeJson.getString("node_code");
        this.nodeName = nodeJson.getString("node_name");
        this.nodeType = nodeJson.getString("node_type");
        this.hostCode = nodeJson.getString("host_code");
        this.appCode = nodeJson.getString("app_code");
        this.protocol = nodeJson.getString("protocol");
        this.basePath = nodeJson.getString("base_path");
        this.servicePort = nodeJson.getString("service_port");
        JSONObject attr = nodeJson.getJSONObject("attr");
        if(attr!=null){
            String url = attr.getString("url");
            String dbName = attr.getString("db_name");
            String dbUser = attr.getString("db_user");
            String dbPwd = attr.getString("db_pwd");
            String redisPwd = attr.getString("redis_pwd");
            this.url = url==null?"":url;
            this.dbName = dbName==null?"":dbName;
            this.dbUser = dbUser==null?"":dbUser;
            this.dbPwd = dbPwd==null?"":dbPwd;
            this.redisPwd = redisPwd==null?"":redisPwd;
        }
        this.remark = nodeJson.getString("remark");
        this.status = nodeJson.getIntValue("status");
        JSONArray tasks = nodeJson.getJSONArray("tasks");
        if(tasks!=null){
            for(int i=0;i<tasks.size();i++){
                this.tasks.add(new TaskNew(tasks.getJSONObject(i)));
            }
        }
    }

    public String getNodeCode() {
        return nodeCode;
    }

    public void setNodeCode(String nodeCode) {
        this.nodeCode = nodeCode;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public String getNodeType() {
        return nodeType;
    }

    public void setNodeType(String nodeType) {
        this.nodeType = nodeType;
    }

    public String getHostCode() {
        return hostCode;
    }

    public void setHostCode(String hostCode) {
        this.hostCode = hostCode;
    }

    public String getAppCode() {
        return appCode;
    }

    public void setAppCode(String appCode) {
        this.appCode = appCode;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String getServicePort() {
        return servicePort;
    }

    public void setServicePort(String servicePort) {
        this.servicePort = servicePort;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPwd() {
        return dbPwd;
    }

    public void setDbPwd(String dbPwd) {
        this.dbPwd = dbPwd;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<TaskNew> getTasks() {
        return tasks;
    }

    public void setTasks(List<TaskNew> tasks) {
        this.tasks = tasks;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getRedisPwd() {
        return redisPwd;
    }

    public void setRedisPwd(String redisPwd) {
        this.redisPwd = redisPwd;
    }

    public String getAppName(){
        AppService appService = (AppService)SpringContextUtil.getBean("appService");
        App app = appService.getByAppCode(this.getAppCode());
        return app==null?"data not found":app.getAppName();
    }


    public String getHostName(){
        HostService hostService = (HostService)SpringContextUtil.getBean("hostService");
        Host host = hostService.getByCode(this.getHostCode());
        return host==null?"data not found":host.getHostName();
    }

    public String getHostIp(){
        HostService hostService = (HostService)SpringContextUtil.getBean("hostService");
        Host host = hostService.getByCode(this.getHostCode());
        return host==null?"data not found":host.getHostIp();
    }

}
